# -*- coding: utf-8 -*-


import logging


def basic_logging_usage():
    '''需要注意的有：
    1 logging中有一些module级别的日志记录方法，例如logging.debug(),logging.warning()等，他们对应的Logger就是logging包下名为root的Logger。logging.root==logging.getLogger()，可以查看源代码。
    2 一旦调用了logging.debug(),logging.warnning()等方法后，logging.root.handlers将由空变为包含一个值，这样以后再调用basicConfig时，因为要判断len(root.handlers)==0，所以调用basicConfig不起作用。
    
    '''
    print('logging basic useage and logging level priority: CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET \n')
    print('logging.getLogger() = %s'%logging.getLogger())
    print('logging.getLoggerClass() = %s'%logging.getLoggerClass())
    print('logging.root == logging.getLogger() ? %s'%(logging.root == logging.getLogger()))
    print('-'*30)
    logging.debug('debug message')
    logging.info('info message')
    logging.warning('warning message')
    logging.error('error message')
    logging.critical('critical message')
    print('-'*30)
    print('logging.basicConfig(level=logging.DEBUG)')
    #logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',datefmt='%a, %d %b %Y %H:%M:%S',filename='tmp_file.log',filemode='w')
    logging.basicConfig(level=logging.DEBUG)
    logging.debug('debug message')
    logging.info('info message')
    logging.warning('warning message')
    logging.error('error message')
    logging.critical('critical message')
    print('log level did not change after call logging.basicConfig(level=logging.DEBUG)')
    print('-'*30)
    print('logging.root.handlers=[]')
    print('you should clear logging.root.handlers before reset logging level')
    logging.root.handlers=[]
    logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',datefmt='%a, %d %b %Y %H:%M:%S',filename='e:\\tmp_file.log',filemode='w')
    logging.debug('debug message')
    logging.info('info message')
    logging.warning('warning message')
    logging.error('error message')
    logging.critical('critical message')
    print('please check file e:\\tmp_file.log')


    print('logging.root.handlers = %s'%logging.root.handlers)
    
    
def advandced_logging_usage():
    '''
    refer to http://blog.chinaunix.net/uid-26000296-id-4372063.html
    '''
    print('\nadvandced_logging_usage')
    logger = logging.getLogger()

    logger1 = logging.getLogger('mylogger')
    logger1.setLevel(logging.DEBUG)

    logger2 = logging.getLogger('mylogger')
    logger2.setLevel(logging.INFO)

    logger3 = logging.getLogger('mylogger.child1')
    logger3.setLevel(logging.WARNING)

    logger4 = logging.getLogger('mylogger.child1.child2')
    logger4.setLevel(logging.DEBUG)

    logger5 = logging.getLogger('mylogger.child1.child2.child3')
    logger5.setLevel(logging.DEBUG)

    # 创建一个handler，用于写入日志文件
    fh = logging.FileHandler('d:\\temp\\test.log')

    # 再创建一个handler，用于输出到控制台
    ch = logging.StreamHandler()

    # 定义handler的输出格式formatter
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)

    #定义一个filter
    #filter = logging.Filter('mylogger.child1.child2')
    #fh.addFilter(filter)

    # 给logger添加handler
    #logger.addFilter(filter)
    logger.addHandler(fh)
    logger.addHandler(ch)

    #logger1.addFilter(filter)
    logger1.addHandler(fh)
    logger1.addHandler(ch)

    logger2.addHandler(fh)
    logger2.addHandler(ch)

    #logger3.addFilter(filter)
    logger3.addHandler(fh)
    logger3.addHandler(ch)

    #logger4.addFilter(filter)
    logger4.addHandler(fh)
    logger4.addHandler(ch)

    logger5.addHandler(fh)
    logger5.addHandler(ch)

    # 记录一条日志
    logger.debug('logger debug message')
    logger.info('logger info message')
    logger.warning('logger warning message')
    logger.error('logger error message')
    logger.critical('logger critical message')

    logger1.debug('logger1 debug message')
    logger1.info('logger1 info message')
    logger1.warning('logger1 warning message')
    logger1.error('logger1 error message')
    logger1.critical('logger1 critical message')

    logger2.debug('logger2 debug message')
    logger2.info('logger2 info message')
    logger2.warning('logger2 warning message')
    logger2.error('logger2 error message')
    logger2.critical('logger2 critical message')

    logger3.debug('logger3 debug message')
    logger3.info('logger3 info message')
    logger3.warning('logger3 warning message')
    logger3.error('logger3 error message')
    logger3.critical('logger3 critical message')

    logger4.debug('logger4 debug message')
    logger4.info('logger4 info message')
    logger4.warning('logger4 warning message')
    logger4.error('logger4 error message')
    logger4.critical('logger4 critical message')

    logger5.debug('logger5 debug message')
    logger5.info('logger5 info message')
    logger5.warning('logger5 warning message')
    logger5.error('logger5 error message')
    logger5.critical('logger5 critical message') 

if __name__=='__main__':
    print('logging usage')
    print('basic_logging_usage()')
    basic_logging_usage()
    print('*'*30)
    advandced_logging_usage()